package controllers;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.CategoryModel;
import models.GeoModel;

import data.CategoryData;
import data.DealData;
import data.GeoData;

@SuppressWarnings("serial")
public class CityServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		int page = 1;
		try {
			page = Integer.parseInt((String) request.getParameter("page"));
		} catch (Exception e) {
			page = 1;
		}
		response.setCharacterEncoding("UTF-8");
		String[] splitted = request.getRequestURI().split("/");
		String city = splitted[1];
		GeoData geoData = new GeoData();
		if (splitted.length == 4) {
			String category = splitted[2];
			String subCategory = splitted[3];
			System.out.println(category);
			System.out.println(subCategory);
		} else if (splitted.length == 3) {
			String category = splitted[2];
			DealData dealData = new DealData();
			CategoryData cateData = new CategoryData();
			GeoModel geo = geoData.getGeoByAlias(city);
			if (category != null && category.equals("deal-hom-nay")) {
				dealData.setPage(page);
				dealData.getTodayDeal(geo.getGeoId());				
				if (dealData.getListResult() != null) {
					Cookie cookie = new Cookie("city", geo.getAlias());
					cookie.setMaxAge(60 * 60 * 24);
					response.addCookie(cookie);
					request.setAttribute("activeCategory", "deal-hom-nay");
					request.setAttribute("recentCategory", "all");
					request.setAttribute("dealData", dealData);
					request.setAttribute("url", request.getRequestURL()
							.toString());
					request.getRequestDispatcher("/pages/home_page.jsp")
							.forward(request, response);
				} else {
					request.getRequestDispatcher("/pages/error.jsp").forward(
							request, response);
				}
			} else if (category != null && category.equals("deal-gan-day")) {
				dealData.setPage(page);
				dealData.getRecentDeal(geo.getGeoId());
				if (dealData.getListResult() != null) {
					Cookie cookie = new Cookie("city", geo.getAlias());
					cookie.setMaxAge(60 * 60 * 24);
					response.addCookie(cookie);
					request.setAttribute("activeCategory", "deal-gan-day");
					request.setAttribute("recentCategory", "all");
					request.setAttribute("dealData", dealData);
					request.setAttribute("url", request.getRequestURL()
							.toString());
					request.getRequestDispatcher("/pages/home_page.jsp")
							.forward(request, response);
				} else {
					request.getRequestDispatcher("/pages/error.jsp").forward(
							request, response);
				}
			} else {
				CategoryModel cate = cateData.getCategoryByAlias(category);
				if (geo != null && cate != null) {
					System.out.println(cate.getAlias()); 
					dealData.setPage(page);
					dealData.getByCityAndCategory(geo.getGeoId(),
							cate.getCategoryId());
					if (dealData.getListResult() != null) {
						Cookie cookie = new Cookie("city", geo.getAlias());
						cookie.setMaxAge(60 * 60 * 24);
						response.addCookie(cookie);
						request.setAttribute("recentCategory",
								cate.getCategoryId());
						request.setAttribute("dealData", dealData);
						request.setAttribute("url", request.getRequestURL()
								.toString());
						request.getRequestDispatcher("/pages/home_page.jsp")
								.forward(request, response);
					} else {
						request.getRequestDispatcher("/pages/error.jsp")
								.forward(request, response);
					}
				} else {
					request.getRequestDispatcher("/pages/error.jsp").forward(
							request, response);
				}
			}

		} else if (splitted.length == 2) {
			DealData dealData = new DealData();
			GeoModel geo = geoData.getGeoByAlias(city);
			if (geo != null) {
				dealData.setPage(page);
				dealData.getByCity(geo.getGeoId());
				Cookie cookie = new Cookie("city", geo.getAlias());
				cookie.setMaxAge(60 * 60 * 24);
				response.addCookie(cookie);
				request.setAttribute("city", geo.getAlias());
				request.setAttribute("recentCategory", "all");
				request.setAttribute("dealData", dealData);
				request.setAttribute("url", request.getRequestURL().toString());
				request.getRequestDispatcher("/pages/home_page.jsp").forward(
						request, response);
			} else {
				request.getRequestDispatcher("/pages/error.jsp").forward(
						request, response);
			}

		}
	}
}
